# Maintainer: Evangelos Foutras <evangelos@foutrelis.com>

pkgname=('geoip-database' 'geoip-database-extra')
pkgver=20230414
pkgrel=1
arch=('any')
url="https://mailfud.org/geoip-legacy/"
license=('CCPL:by-sa')
makedepends=('util-linux')
checkdepends=('geoip')
_source_base=https://sources.archlinux.org/other/packages/$pkgname/$pkgver
source=(GeoIP-$pkgver.dat.gz::$_source_base/GeoIP.dat.gz
        GeoIPv6-$pkgver.dat.gz::$_source_base/GeoIPv6.dat.gz
        GeoIPCity-$pkgver.dat.gz::$_source_base/GeoIPCity.dat.gz
        GeoIPCityv6-$pkgver.dat.gz::$_source_base/GeoIPCityv6.dat.gz
        GeoIPASNum-$pkgver.dat.gz::$_source_base/GeoIPASNum.dat.gz
        GeoIPASNumv6-$pkgver.dat.gz::$_source_base/GeoIPASNumv6.dat.gz)
sha256sums=('aca8437106a8d0b968e0fa8473815fa4c83e1a06b43489eb6135da908f3333a5'
            '2f465e5477c88391adb2928c1e6f486f76693f3af98b7140a68dac812ce90c4e'
            '85405741234242b925389879f4065d1d11b3f13b224a5ab68139a87d32f8727e'
            'b551e15c0d56331cee0734cfb291098016c4847734bead5b8e283275d268a310'
            '61a87bbf3a50f0493a2a22bd64d878bfda2f2dd576e2f28de839df1bc0f5a2a6'
            'a3e8602efbcd689de36c628838d5dfad79c0c927801d4e0555e218cd658814c7')

prepare() {
  cd "$srcdir"
  rename -v -- "-$pkgver" '' *.dat
}

check() {
  cd "$srcdir"

  if [[ $(geoiplookup -f GeoIP.dat 8.8.8.8) != *'US, United States' ]]; then
    echo >&2 'Unable to resolve IPv4 address to country.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPv6.dat 2001:4860:4860::8888) != *'US, United States' ]]; then
    echo >&2 'Unable to resolve IPv6 address to country.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPCity.dat 8.8.8.8) != *'US, CA, California, Los Angeles'* ]]; then
    echo >&2 'Unable to resolve IPv4 address to city.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPCityv6.dat 2001:4860:4860::8888) != *'US, 00, N/A, N/A'* ]]; then
    echo >&2 'Unable to resolve IPv6 address to city.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPASNum.dat 8.8.8.8) != *'AS15169 GOOGLE' ]]; then
    echo >&2 'Unable to resolve IPv4 address to ASN.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPASNumv6.dat 2001:4860:4860::8888) != *'AS15169 GOOGLE' ]]; then
    echo >&2 'Unable to resolve IPv6 address to ASN.'
    return 1
  fi
}

package_geoip-database() {
  pkgdesc="GeoIP legacy country database (based on GeoLite2 data created by MaxMind)"

  cd "$srcdir"

  install -d "$pkgdir/usr/share/GeoIP"
  install -m644 -t "$pkgdir/usr/share/GeoIP" GeoIP{,v6}.dat
}

package_geoip-database-extra() {
  pkgdesc="GeoIP legacy city/ASN databases (based on GeoLite2 data created by MaxMind)"

  cd "$srcdir"

  install -d "$pkgdir/usr/share/GeoIP"
  install -m644 -t "$pkgdir/usr/share/GeoIP" GeoIPCity{,v6}.dat GeoIPASNum{,v6}.dat
}

# vim:set ts=2 sw=2 et:
